package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.User;
import com.qf.entity.UserDTO;
import com.qf.service.UserService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ------回首昨日,更进一步------
 *
 * @author JinXiaoChen
 * @date 2025/4/27
 * @desc
 */
@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询所有用户信息
     *
     * @return
     */
    @PostMapping("/find/user/All")
    public R findAll() {
        List<User> users = userService.findAll();
        return R.ok(users);
    }



    /**
     * 分页模糊查询用户信息
     */
    @GetMapping("/find/user/page")
    public R findPage(UserDTO userDto) {
        PageHelper.startPage(userDto.getPageNum(), userDto.getPageSize());
        List<User> users = userService.findPage(userDto);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return R.ok(pageInfo);
    }



    /**
     * 增加用户信息
     */
    @GetMapping("/user/add")
    public R addUser(User user) {
        user.setRole("USER");
        boolean flag = userService.addUser(user);
        return R.ok(flag);
    }

    /**
     * 删除用户信息
     */
    @GetMapping("/user/delete")
    public R deleteUser(Integer id) {
        boolean flag = userService.deleteUser(id);
        return R.ok(flag);
    }

    /**
     * 批量删除用户信息
     */
    @PostMapping("/user/delete/batch")
    public R deleteUserBatch(@RequestBody List<Integer> ids) {
        boolean flag = userService.deleteUserBatch(ids);
        return R.ok(flag);
    }

    /**
     * 修改用户信息
     */
    @GetMapping("/user/update")
    public R updateUser(User user) {
        boolean flag = userService.updateUser(user);
        return R.ok(flag);
    }

}
